﻿using System;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class NatSelTableEdit : Form
    {
        private Model model;
        private NaturalSelectionTable natsel_t;
        private NaturalSelectionLink natsel_link = null, def_natsel_link=null;
        private ReproductionLink reprod_link = null;
        public NatSelTableEdit(Model m, NaturalSelectionTable t)
        {
            InitializeComponent();
            model = m;
            natsel_t = t;
        }
        private void update()
        {
            natsel_t = new NaturalSelectionTable(natsel_t.path);
            variants_box.Items.Clear();
            foreach (NaturalSelectorParametersArea area in natsel_t.natural_selection_table.Keys)
            {
                variants_box.Items.Add(area.id);
            }
        }
        private void NatSelTableEdit_Load(object sender, EventArgs e)
        {
            name_box.Text = natsel_t.name;
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if (link.path_to_phenotype_table == natsel_t.path_to_phenotype_table)
                {
                    natsel_link_box.Items.Add(link.name);
                    def_natsel_link_box.Items.Add(link.name);
                }
                if (link.path == natsel_t.path_to_default_natural_selection_link)
                    def_natsel_link_box.SelectedItem = link.name;
            }            
            update();
        }

        private void variants_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (variants_box.SelectedItem != null)
            {
                NaturalSelectorParametersArea area = null;
                foreach (NaturalSelectorParametersArea a in natsel_t.natural_selection_table.Keys)
                {
                    if (a.id == (int)variants_box.SelectedItem)
                    {
                        area = a;
                        break;
                    }
                }
                low_border.Value = area.amount_of_individuals_lower_bound;
                upper_border.Value = area.amount_of_individuals_upper_bound;
                first_step.Value = area.first_time_step;
                last_step.Value = area.last_time_step;
                try
                {
                    natsel_link_box.SelectedItem = model.model_natural_selection_links[natsel_t.natural_selection_table[area].path_to_natural_selection_link].name;
                    reprod_link_box.SelectedItem = model.model_reproduction_links[natsel_t.natural_selection_table[area].path_to_reproduction_link].name;
                    natsel_link = model.model_natural_selection_links[natsel_t.natural_selection_table[area].path_to_natural_selection_link];
                    reprod_link = model.model_reproduction_links[natsel_t.natural_selection_table[area].path_to_reproduction_link];
                }
                catch { }
            }
        }

        private void set_butt_Click(object sender, EventArgs e)
        {
            if ((variants_box.SelectedItem != null)&&(natsel_link_box.SelectedItem!=null)&&(reprod_link_box.SelectedItem!=null))
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(natsel_t.path));
                foreach (XmlNode node in doc.GetElementsByTagName("natural_selection_variant"))
                {
                    if (node.Attributes[0].Value == variants_box.SelectedItem.ToString())
                    {
                        node.Attributes[1].Value = first_step.Value.ToString();
                        node.Attributes[2].Value = last_step.Value.ToString();
                        node.Attributes[3].Value = low_border.Value.ToString();
                        node.Attributes[4].Value = upper_border.Value.ToString();
                        node.Attributes[5].Value = natsel_link.path;
                        node.Attributes[6].Value = reprod_link.path;
                        break;
                    }
                }
                doc.Save(natsel_t.path);
                int ind = variants_box.SelectedIndex;
                update();
                variants_box.SelectedIndex = ind;
            }
        }

        private void natsel_link_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (NaturalSelectionLink l in model.model_natural_selection_links.Values)
            {
                if (l.name == (string)natsel_link_box.SelectedItem)
                {
                    natsel_link = l;
                    break;
                }
            }
            reprod_link_box.Enabled = true;
            reprod_link_box.Items.Clear();
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if(link.path_to_natsel_link==natsel_link.path)
                reprod_link_box.Items.Add(link.name);
            }
            reprod_link_box.SelectedIndex = 0;
        }

        private void reprod_link_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ReproductionLink l in model.model_reproduction_links.Values)
            {
                if (l.name == (string)reprod_link_box.SelectedItem)
                {
                    reprod_link = l;
                    break;
                }
            }
        }

        private void add_butt_Click(object sender, EventArgs e)
        {
            NewVariant new_var = new NewVariant(model,natsel_t);
            if (new_var.ShowDialog() == DialogResult.OK)
                update();
        }

        private void remove_butt_Click(object sender, EventArgs e)
        {
            if (variants_box.SelectedItem != null)
            {
                MyXmlDocument doc = new MyXmlDocument();
                doc.LoadXml(System.IO.File.ReadAllText(natsel_t.path));
                foreach (XmlNode node in doc.GetElementsByTagName("natural_selection_variant"))
                {
                    if (node.Attributes[0].Value == variants_box.SelectedItem.ToString())
                    {
                        node.ParentNode.RemoveChild(node);
                    }
                    doc.Decrement(doc.DocumentElement.Attributes[1]);
                    break;
                }
                update();
                variants_box.SelectedItem = null;
                low_border.Value = 0;
                upper_border.Value = 0;
                first_step.Value = 0;
                last_step.Value = 0;
                natsel_link_box.SelectedItem = null;
                reprod_link_box.SelectedItem = null;
                natsel_link = null;
                reprod_link = null;
            }
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(natsel_t.path));
            doc.DocumentElement.Attributes[0].Value = name_box.Text;
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if ((string)def_reproduc_link_box.SelectedItem == link.name)
                {
                    doc.DocumentElement.Attributes[4].Value=link.path;
                    break;
                }
            }
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if ((string)def_natsel_link_box.SelectedItem == link.name)
                {
                    doc.DocumentElement.Attributes[3].Value = link.path;
                    break;
                }
            }
            doc.Save(natsel_t.path);
            this.Close();
        }

        private void def_natsel_link_box_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
            {
                if ((string)def_natsel_link_box.SelectedItem == link.name)
                {
                    def_natsel_link = link;
                    break;
                }
            }
            def_reproduc_link_box.Items.Clear();
            foreach (ReproductionLink link in model.model_reproduction_links.Values)
            {
                if (link.path_to_natsel_link ==def_natsel_link.path)
                    def_reproduc_link_box.Items.Add(link.name);
            }
            def_reproduc_link_box.SelectedIndex = 0;
        }
    }
}
